package com.wei.mybatis.controller;


import com.wei.mybatis.bean.User;
import com.wei.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public String register(User user){
        userService.register(user);
        return "add_success.html";
    }

    @GetMapping("/all")
    public String findAll(Model model){
        List<User> users = userService.findAll();
        model.addAttribute("all",users);
        return "index.html";
    }

    @GetMapping("/delete")
    public String delete(String id){
        userService.delete(id);
        return "redirect:all";
    }
}
